package bb.lanxing.chart.sample;

import com.github.mikephil.charting.utils.Utils;

import java.util.ArrayList;
import java.util.List;


import bb.lanxing.chart.bean.IPoint;
import bb.lanxing.util.DistanceUtil;
import bb.lanxing.util.Log;

public abstract class LineChartDataDistanceSample<T extends IPoint> implements ILineChartDataSampler<T> {
    final private static String TAG = "LineChartDataDistanceSample";
    private List<Double> distanceList;
    private List<T> sampleDataSourceList;

    protected abstract boolean ignoreDistance(T lastPoint, T point);

    protected abstract boolean isExtremaIndex(int index);

    public List<Double> getSampledDistance() {
        return this.distanceList;
    }

    public List<T> getSampleDataSource() {
        return this.sampleDataSourceList;
    }

    public void sampleData(double totalDistance, List<? extends T> dataSource, int sampleSize) {
        Log.d(TAG, "sampleData...");
        if (dataSource == null || dataSource.size() == 0) {
            return;
        }
        Log.d(TAG, "sampleData...dataSource size=" + dataSource.size() + ", sampleSize=" + sampleSize);

        this.distanceList = new ArrayList<>();
        this.sampleDataSourceList = new ArrayList<>();
        int dataSize = dataSource.size();
        if (dataSize <= sampleSize) {
            double vDistance = (double) Utils.DOUBLE_EPSILON;
            this.distanceList.add((double) Utils.DOUBLE_EPSILON);
            this.sampleDataSourceList.add(dataSource.get(0));
            for (int i = 1; i < dataSource.size(); i++) {
                T t = dataSource.get(i - 1);
                T t2 = dataSource.get(i);
                if (!ignoreDistance(t, t2)) {
                    vDistance = vDistance + DistanceUtil.get(t.getLatitude(), t.getLongitude(), t2.getLatitude(), t2.getLongitude());
                    this.sampleDataSourceList.add(t2);
                    this.distanceList.add(vDistance);
                }
            }
        } else {
            double[] distanceData = new double[dataSize];
            distanceData[0] = 0.0d;
            findExtremePoint(dataSource);

            double d = 0.0d;
            int i2 = 0;
            while (i2 < dataSource.size() - 1) {
                T t3 = dataSource.get(i2);
                i2++;
                T t4 = dataSource.get(i2);
                if (!ignoreDistance(t3, t4)) {
                    d += DistanceUtil.get(t3.getLatitude(), t3.getLongitude(), t4.getLatitude(), t4.getLongitude());
                }
                distanceData[i2] = d;
            }

            double d2 = totalDistance / d;
            double d3 = totalDistance / sampleSize;
            T t5 = dataSource.get(0);
            sampleDataSourceList.add(t5);
            distanceList.add((double) Utils.DOUBLE_EPSILON);
            double d4 = d3;
            int i3 = 1;
            while (i3 < dataSize) {
                T t6 = dataSource.get(i3);
                if (!ignoreDistance(t5, t6)) {
                    if (isExtremaIndex(i3) || i3 == dataSize - 1) {
                        distanceList.add(distanceData[i3] * d2);
                        sampleDataSourceList.add(t6);
                    } else if (distanceData[i3] >= d4) {
                        distanceList.add(distanceData[i3] * d2);
                        sampleDataSourceList.add(t6);
                    } else if (distanceData[i3] < d4) {
                        int i4 = i3 + 1;
//                    double vCheckDistance = d4;
//                    double vDistance = dArr[i4];
                        if (d4 <= distanceData[i4]) {
                            if (distanceData[i4] - d4 >= d4 - distanceData[i3]) {
                                sampleDataSourceList.add(t6);
                                distanceList.add(distanceData[i3] * d2);
                            } else {
                                sampleDataSourceList.add(dataSource.get(i4));
                                distanceList.add(distanceData[i4] * d2);
                                d4 = d4 + (2.0d * d3) + d3;
                                i3 = i4;
//                            t5 = dataSource.get(i4);
                                i3++;
                            }
                            d4 += d3;
                        }
                    }
                }
                t5 = t6;
                i3++;
            }
        }
    }
}
